package kbl.language;

class NFAState implements Comparable<NFAState> {
	protected int id = Integer.MAX_VALUE;
	private NFAState out;
	private NFAState altout;
	private char c;
	private CharacterClass cc;
	protected boolean match = false;
	protected boolean epsilon = true;

	char getChar() {
		return this.c;
	}

	CharacterClass getCC() {
		return this.cc;
	}

	NFAState(StrategyRE2NFA sre, char c) {
		this(sre);
		this.c = c;
		this.epsilon = false;
	}

	NFAState() {
	}

	NFAState(StrategyRE2NFA sre, CharacterClass cc) {
		this(sre);
		this.cc = cc;
		this.epsilon = false;
	}

	NFAState(StrategyRE2NFA sre) {
		this.id = sre.getStatecount();
		sre.incStatecount();
	}

	void setOut(NFAState out) {
		this.out = out;
	}

	NFAState getOut() {
		return out;
	}

	void setAltout(NFAState altout) {
		this.altout = altout;
	}

	NFAState getAltout() {
		return altout;
	}

	boolean match(char c) {
		if (this.c == c) {
			return true;
		} else if (cc != null) {
			return cc.contains(c);
		} else {
			return false;
		}
	}

	boolean isMatch() {
		return match;
	}

	boolean isEpsilon() {
		return epsilon;
	}

	int getid() {
		return id;
	}

	private String ccString() {
		if (cc != null) {
			return cc.toString();
		} else {
			return Character.toString(c);

		}
	}

	@Override
	public int hashCode() {
		return id;
	}

	@Override
	public int compareTo(NFAState o) {
		return o.id - this.id;
	}

	@Override
	public String toString() {
		return id + ccString();
	}

}

class Match extends NFAState {

	private String name;

	Match(StrategyRE2NFA sre, String name) {
		this.match = true;
		this.epsilon = false;
		this.name = name;
		this.id = sre.getMatchcount();
		sre.incMatchcount();
	}

	String getName() {
		return name;
	}

	@Override
	public String toString() {
		return name;
	}

}
